import Vue from 'vue'
import Vuex from 'vuex'

import { LoginAPI } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || ''
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
    }
  },
  actions: {
    async asyncLoginAction (store, val) {
      try {
        const res = await LoginAPI(val)
        console.log(res)
        const { token } = res.data.body
        store.commit('setToken', token)

        return Promise.resolve(res) // 返回成功的Promise对象
      } catch (err) {
        return Promise.reject(err) // 返回失败的Promise对象
      }
    }

  },
  modules: {
  }
})
